package com.tenbit.tbw.ctl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.tenbit.tbw.svc.EMP1010SVC;
import com.tenbit.tbw.svc.PDT1010SVC;
import com.tenbit.tbw.vo.PDT1010VO;

@Controller
public class PDT1010CTL {
	
	@Autowired
	private PDT1010SVC svc;
	
	@Autowired
	private EMP1010SVC svc1;
	
	private Logger log=Logger.getLogger(this.getClass());
	
	private Map<String,Object> paramMap= new HashMap<String,Object>();
	private Map<String,Object> resultMap = new HashMap<String,Object>();
	
//	@RequestMapping("PDT1010M1.do")
	public ModelAndView getProductInfoList(HttpServletRequest request)
	{
		log.debug(" ==============PDT1010M1.do-getProductInfoList:초기 상품정보 목록 ==============");
		ModelAndView mav = new ModelAndView();
		int page=1;
		int limit=10;
	
		
		if(request.getParameter("page") != null)
		{
			page = Integer.parseInt(request.getParameter("page"));
		}
		int startrow = (page-1)*10+1;
		int endrow=startrow+10-1;
		String sellStartDate1 = request.getParameter("sellStartDate1");
		String sellStartDate2 = request.getParameter("sellStartDate2");
		String productName=request.getParameter("productName");
		
		paramMap.put("sellStartDate1", sellStartDate1);
		paramMap.put("sellStartDate2", sellStartDate2);
		paramMap.put("productName",productName);
		paramMap.put("page",page);
		paramMap.put("startrow",startrow);
		paramMap.put("endrow", endrow);
		
		int listcount=0;
	
		resultMap=svc.getProductInfoList(paramMap);
		
		String msg=(String) resultMap.get("msg");
		
		if(resultMap.get("productInfoList")!=null){
			List<PDT1010VO> list = (List<PDT1010VO>) resultMap.get("productInfoList");
			mav.addObject("list",list);
			
		}
		
		listcount=(Integer) svc.getProductInfoListCount(paramMap).get("listCount");
		
		int maxpage = (int)((double)listcount/limit+0.95);
		int startpage =(((int)((double)page / 10+0.9))-1)*10+1;
		if(!(Boolean) resultMap.get("result")){
			mav.addObject("msg",msg);
		}
		
		
		String resultStartDate1=(String) resultMap.get("sellStartDate1");
		String resultStartDate2=(String) resultMap.get("sellStartDate2");
		
		log.debug(resultMap);
		mav.addObject("page",page);
		mav.addObject("maxpage",maxpage);
		mav.addObject("startpage",startpage);
		mav.addObject("sellStartDate1",resultStartDate1);
		mav.addObject("sellStartDate2",resultStartDate2);
		mav.addObject("productName",productName);
		mav.setViewName("2nd_week/PDT1010M1");
		return mav;
	}
}
